/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
import org.orm.*;
import org.hibernate.Query;
import java.util.List;

public class CuestionarioDAO {
	public static Cuestionario loadCuestionarioByORMID(int cod_cuestionario) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return loadCuestionarioByORMID(session, cod_cuestionario);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario getCuestionarioByORMID(int cod_cuestionario) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return getCuestionarioByORMID(session, cod_cuestionario);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByORMID(int cod_cuestionario, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return loadCuestionarioByORMID(session, cod_cuestionario, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario getCuestionarioByORMID(int cod_cuestionario, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return getCuestionarioByORMID(session, cod_cuestionario, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByORMID(PersistentSession session, int cod_cuestionario) throws PersistentException {
		try {
			return (Cuestionario) session.load(Cuestionario.class, new Integer(cod_cuestionario));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario getCuestionarioByORMID(PersistentSession session, int cod_cuestionario) throws PersistentException {
		try {
			return (Cuestionario) session.get(Cuestionario.class, new Integer(cod_cuestionario));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByORMID(PersistentSession session, int cod_cuestionario, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Cuestionario) session.load(Cuestionario.class, new Integer(cod_cuestionario), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario getCuestionarioByORMID(PersistentSession session, int cod_cuestionario, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Cuestionario) session.get(Cuestionario.class, new Integer(cod_cuestionario), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario[] listCuestionarioByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return listCuestionarioByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario[] listCuestionarioByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return listCuestionarioByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario[] listCuestionarioByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From Cuestionario as Cuestionario");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Cuestionario[]) list.toArray(new Cuestionario[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario[] listCuestionarioByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From Cuestionario as Cuestionario");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Cuestionario[]) list.toArray(new Cuestionario[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return loadCuestionarioByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return loadCuestionarioByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Cuestionario[] cuestionarios = listCuestionarioByQuery(session, condition, orderBy);
		if (cuestionarios != null && cuestionarios.length > 0)
			return cuestionarios[0];
		else
			return null;
	}
	
	public static Cuestionario loadCuestionarioByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Cuestionario[] cuestionarios = listCuestionarioByQuery(session, condition, orderBy, lockMode);
		if (cuestionarios != null && cuestionarios.length > 0)
			return cuestionarios[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateCuestionarioByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return iterateCuestionarioByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateCuestionarioByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = BasededatosPersistentManager.instance().getSession();
			return iterateCuestionarioByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateCuestionarioByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From Cuestionario as Cuestionario");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateCuestionarioByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From Cuestionario as Cuestionario");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario createCuestionario() {
		return new Cuestionario();
	}
	
	public static boolean save(Cuestionario cuestionario) throws PersistentException {
		try {
			BasededatosPersistentManager.instance().saveObject(cuestionario);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean delete(Cuestionario cuestionario) throws PersistentException {
		try {
			BasededatosPersistentManager.instance().deleteObject(cuestionario);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean deleteAndDissociate(Cuestionario cuestionario)throws PersistentException {
		try {
			Medico[] lMedicos = cuestionario.medico.toArray();
			for(int i = 0; i < lMedicos.length; i++) {
				lMedicos[i].cuestionario.remove(cuestionario);
			}
			Pregunta[] lPreguntas = cuestionario.pregunta.toArray();
			for(int i = 0; i < lPreguntas.length; i++) {
				lPreguntas[i].cuestionario.remove(cuestionario);
			}
			return delete(cuestionario);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean deleteAndDissociate(Cuestionario cuestionario, org.orm.PersistentSession session)throws PersistentException {
		try {
			Medico[] lMedicos = cuestionario.medico.toArray();
			for(int i = 0; i < lMedicos.length; i++) {
				lMedicos[i].cuestionario.remove(cuestionario);
			}
			Pregunta[] lPreguntas = cuestionario.pregunta.toArray();
			for(int i = 0; i < lPreguntas.length; i++) {
				lPreguntas[i].cuestionario.remove(cuestionario);
			}
			try {
				session.delete(cuestionario);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean refresh(Cuestionario cuestionario) throws PersistentException {
		try {
			BasededatosPersistentManager.instance().getSession().refresh(cuestionario);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean evict(Cuestionario cuestionario) throws PersistentException {
		try {
			BasededatosPersistentManager.instance().getSession().evict(cuestionario);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Cuestionario loadCuestionarioByCriteria(CuestionarioCriteria cuestionarioCriteria) {
		Cuestionario[] cuestionarios = listCuestionarioByCriteria(cuestionarioCriteria);
		if(cuestionarios == null || cuestionarios.length == 0) {
			return null;
		}
		return cuestionarios[0];
	}
	
	public static Cuestionario[] listCuestionarioByCriteria(CuestionarioCriteria cuestionarioCriteria) {
		return cuestionarioCriteria.listCuestionario();
	}
}
